/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.infrastructure.message.util;

import cn.hallele.infrastructure.message.io.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;

import java.util.UUID;

/**
 * MQ工具类
 *
 * @author anle5
 * @since 2024/9/14 21:35
 */
@Slf4j
public class MqUtil {
    private static final StreamBridge STREAM_BRIDGE;

    static {
        STREAM_BRIDGE = SpringUtil.getBean(StreamBridge.class);
    }

    /**
     * 发送mq消息
     *
     * @param message 消息体
     * @param <T>     实际的内容
     * @return true if data was sent successfully, otherwise false or throws an exception.
     */
    public static <T> boolean sendMsg(Message<T> message) {
        return STREAM_BRIDGE.send(message.getBindingName(), MessageBuilder.withPayload(message).build());
    }

    /**
     * 生成会话id
     *
     * @return 会话id
     */
    public static String genSessionId() {
        return UUID.randomUUID().toString();
    }
}
